import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import org.apache.commons.codec.binary.Base64;

public class testLottery {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			LotterySecurity a = new LotterySecurity();
			String result = a.verifyLottery("12345678910");
			/*
			KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
		    kpg.initialize(1024);
		    KeyPair keyPair = kpg.genKeyPair();

		    byte[] data = "test".getBytes("UTF8");

		    Signature sig = Signature.getInstance("MD5WithRSA");
		    sig.initSign(keyPair.getPrivate());
		    sig.update(data);
		    byte[] signatureBytes = sig.sign();
		    System.out.println("Singature:" + new String(Base64.encodeBase64(signatureBytes)));

		    sig.initVerify(keyPair.getPublic());
		    sig.update(data);

		    System.out.println(sig.verify(signatureBytes));
			*/
		}
		catch(Exception ex){}
		
/*try {
            
            //*********************** SETUP **********************************
            
            RSAPublicKey pubKey;
            RSAPrivateKey privKey;
            
            //generate the RSA key pair
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            //initialise the KeyGenerator with a random number.
            keyGen.initialize(1024, new SecureRandom());
            KeyPair keypair = keyGen.genKeyPair();
            privKey = (RSAPrivateKey)keypair.getPrivate();
            pubKey = (RSAPublicKey)keypair.getPublic();
        
            String message = "X";
            byte [] raw = message.getBytes("UTF8");
            
            BigInteger m = new BigInteger(raw);
            BigInteger e = pubKey.getPublicExponent();
            BigInteger d = privKey.getPrivateExponent();

            SecureRandom random = SecureRandom.getInstance("SHA1PRNG","SUN");
            byte [] randomBytes = new byte[10];
            BigInteger r = null;
            BigInteger n = pubKey.getModulus();
            BigInteger gcd = null;
            BigInteger one = new BigInteger("1");
            //check that gcd(r,n) = 1 && r < n && r > 1
            do {
                random.nextBytes(randomBytes);
                r = new BigInteger(randomBytes);
                gcd = r.gcd(n);
                System.out.println("gcd: " + gcd);
            }
            while(!gcd.equals(one) || r.compareTo(n)>=0 || r.compareTo(one)<=0);

           //********************* BLIND ************************************
            
            BigInteger b = ((r.modPow(e,n)).multiply(m)).mod(n);
            System.out.println("\nb = " + b);
            //must use modPow() - takes an eternity to compute:
            //b = ((r.pow(e.intValue)).multiply(m)).mod(n);
            
            //********************* SIGN *************************************
            
            BigInteger bs = b.modPow(d,n);
            System.out.println("bs = " + bs);
            
            //********************* UNBLIND **********************************
            
            BigInteger s = r.modInverse(n).multiply(bs).mod(n);
            System.out.println("s = " + s);
            
            //********************* VERIFY ***********************************
            
            //signature of m should = (m^d) mod n
            BigInteger sig_of_m = m.modPow(d,n);
            System.out.println("sig_of_m = " + sig_of_m);
            
            //check that s is equal to a signature of m:
            System.out.println(s.equals(sig_of_m));
            
            //try to verify using the RSA formula
            BigInteger check = s.modPow(e,n);
            System.out.println(m.equals(check));

            //BOTH TESTS RETURN FALSE - s must not be a valid signature of m 
        }
        catch(Exception ex) {
            System.out.println("ERROR: ");
            ex.printStackTrace();
        }*/
	}
	
	

}

